﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ufida.u9.ait.openapi.utils
{
    internal class ObjUtils
    {
        public enum OperEnum
        {
            add,update,delete
        };
        public class OperItem
        {
            //操作状态 新增/修改/删除
            public OperEnum oper;
            //List1对应项目
            public Object item1;
            //List2对应项目
            public Object item2;
            public OperItem(OperEnum oper, object item1, object item2)
            {
                this.oper = oper;
                this.item1 = item1;
                this.item2 = item2;
            }
        }
        /// <summary>
        /// 将字符集合转换为字符串,格式 'arg1','arg2','arg3'
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string StringJoin(IEnumerable<string> args)
        {
            return string.Join(",", args.Select(t => $"'{t}'"));
        }

        /// <summary>
        /// 两个列表作比对,并返回比对后的结果
        /// </summary>
        /// <param name="list1">比对列表1</param>
        /// <param name="list2">比对列表2</param>
        /// <param name="fieldName">比对字段,该字段不能为空</param>
        /// <returns>比对结果LIST,包括 状态(oper)=新增/修改/删除,其中item1存入list1项目,item2存储list2项目</returns>
        public static List<OperItem> CompareList(IList list1, IList list2, String fieldName)
        {
            //返回列表
            List<OperItem> retlist = new List<OperItem>();
            //构造list1字典,key=fieldName值
            Dictionary<String, Object> list1dic = new Dictionary<string, object>();
            foreach (var item in list1)
            {
                //通过反射,获取字段值
                string key = ReflexUtils.GetValue(fieldName, item);
                if (String.IsNullOrEmpty(key) || key == "0")
                    throw new Exception($"对象[{item.GetType().Name}],字段[{fieldName}]不能为空或0");
                list1dic.Add(key, item);
            }
            //构造list2字典,key=fieldName值
            Dictionary<String, Object> list2dic = new Dictionary<string, object>();
            foreach (var item in list2)
            {
                //通过反射,获取字段值
                string key = ReflexUtils.GetValue(fieldName, item);
                if (String.IsNullOrEmpty(key) || key == "0")
                    throw new Exception($"对象[{item.GetType().Name}],字段[{fieldName}]不能为空或0");
                list2dic.Add(key, item);
            }

            foreach (var item in list1)
            {
                //循环list1
                OperItem oper = null;
                string key = ReflexUtils.GetValue(fieldName, item);
                if (list2dic.ContainsKey(key))
                {
                    //list1和list2都存在,返回状态=更新,item1=list1中的项目,item2=list2中的项目
                    oper = new OperItem(OperEnum.update, item, list2dic[key]);
                }
                else
                {
                    //list1存在,list2不存在,返回状态=新增,item1=新增项
                    oper = new OperItem(OperEnum.add, item,null);
                }
                retlist.Add(oper);
            }
            foreach (var item in list2)
            {
                //循环list2
                OperItem oper = null;
                String key = ReflexUtils.GetValue(fieldName, item);
                if (list1dic.ContainsKey(key)) continue;

                //list1不存在,list2存在,返回状态=删除,item2=删除项
                oper=new OperItem(OperEnum.delete,null, item);
                retlist.Add(oper);
            }
            return retlist;
        }
    }
}
